﻿using Glue;
using Glue.Internals;
using Xunit;

namespace GlueTests.MappingTests
{
    public class When_adding_a_relation_between_arrays
    {
        private readonly Mapping<DomainPerson, GuiPerson> mapping;
        private readonly RelationContainer<DomainPerson, GuiPerson> relations;

        public class DomainPerson { public int[] Numbers { get; set; } }
        public class GuiPerson
        {
            public int[] Numbers { get; set; }
            public string[] NumbersAsStrings { get; set; }
        }

        
        public When_adding_a_relation_between_arrays()
        {
            mapping = new Mapping<DomainPerson, GuiPerson>();
            relations = mapping.Relations;
        }

        [Fact]
        public void Should_work()
        {
            mapping.Relate(domain => domain.Numbers, gui => gui.Numbers);
            Assert.Equal(1, relations.Count);
            Assert.Equal(1,relations.GetRelationsTowardsLeft().Count);
            Assert.Equal(1, relations.GetRelationsTowardsRight().Count);
        }

        [Fact]
        public void Should_work_when_converter_is_valid()
        {
            mapping.Relate(domain => domain.Numbers, gui => gui.NumbersAsStrings, Converting.BetweenIntAndString());
        }

        [Fact]
        public void Should_not_work_when_converter_is_wrong()
        {
            Assert.Throws<GlueException>( () =>
                mapping.Relate(domain=>domain.Numbers,gui=>gui.NumbersAsStrings, new SameTypeMapping(typeof(int)))
            );
        }
    }
}
